package hylanda.processors.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;



public class HtmlTagUtil {
	private Pattern p_script = Pattern.compile("<script[^>]*?>[\\s\\S]*?<\\/script>", Pattern.CASE_INSENSITIVE);
	private Pattern p_style = Pattern.compile("<style[^>]*?>[\\s\\S]*?<\\/style>", Pattern.CASE_INSENSITIVE);;
	private Pattern p_html = Pattern.compile("<[^>]+>", Pattern.CASE_INSENSITIVE);

	private Pattern p_punctuation = Pattern.compile("[\\s\\pP\\p{Punct}]",Pattern.CASE_INSENSITIVE);
	/**
	 * 去掉html标签
	 * 
	 * @param htmlStr
	 *            包含html标签的字符串
	 * @return
	 */
	public String delHTMLTag(String htmlStr) {
		//处理转义字符
		//htmlStr =  StringEscapeUtils.unescapeHtml3(htmlStr);
		
		StringBuilder s = new StringBuilder();
		Matcher m_script = p_script.matcher(htmlStr);
		htmlStr = m_script.replaceAll("");

		Matcher m_style = p_style.matcher(htmlStr);
		htmlStr = m_style.replaceAll("");
		
		Matcher m_html = p_html.matcher(htmlStr);
		htmlStr = m_html.replaceAll("");
		return s.append(htmlStr.trim()).toString();
	}

	/**
	 * 去掉标点符号（全角+半角）、空白
	 * @param str
	 * @return
	 */
	public String delPunctuation(String str){
		Matcher m_style = p_punctuation.matcher(str);
		str = m_style.replaceAll("");
		return str;
	}

}
